package com.qk.exception;

import com.qk.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public Result handle(Exception e) {
        log.error("异常是：{}", e.getMessage());
        return Result.error("对不起,您的网络错误");
    }
    @ExceptionHandler
    public Result handle(DuplicateKeyException e) {
        String errmessage = e.getMessage();
        if(errmessage.contains("Duplicate entry")){
            String msg = errmessage.substring(errmessage.indexOf("Duplicate entry"));
            String[] split = msg.split(" ");
            return Result.error("您输入的"+split[2]+"已存在");
        }
        return Result.error("系统错误");

    }
    @ExceptionHandler
    public Result handle(BusinessException e) {
        String errmessage = e.getMessage();
        return Result.error(errmessage);
    }
}
